﻿import os

import cv2

def save_img(video_path,save_pic_path):
    strs = video_path.split("/")
    file_name = strs[len(strs)-1].split(".")[0]

    folder_name = save_pic_path +"/"+ file_name
    os.makedirs(folder_name,True)
    vc = cv2.VideoCapture(video_path) #读入视频文件
    c=0
    rval=vc.isOpened()

    while rval:   #循环读取视频帧
        c = c + 1
        rval, frame = vc.read()
        if rval:
            cv2.imwrite(folder_name +"/"+ str(c) + '.jpg', frame) #存储为图像,保存名为 文件夹名_数字（第几个文件）.jpg
        else:
            break
    vc.release()
    print('video to picture save_success')
    print('save_path:'+ folder_name)

if __name__ == '__main__':
    videopath = r'./video/520521.mp4'
    save_pic_path = r'./img'
    save_img(videopath,save_pic_path)